<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Dangle
 *
 * @author kolin add 2014.08.29  modify by dragon.hu @2016.3.8
 */
require_once 'BaseChannel.php';
require_once 'include/Anzhi/class_desc.php';

///安智支付;
class Anzhi implements BaseChannel {

    ///构造函数;
    function __construct() {
        
    }

    //安卓支付验证;
    function android($data) {
        $paymentKey = "";
        $ret = $this->paymentCallBack($paymentKey, ANDROID);
        echo $ret;
    }

    ///IOS支付验证;
    function ios($data) {
        $paymantKey = "";
        $ret = $this->paymentCallBack($paymantKey, IOS);
    }

    ///支付验证函数;
    function paymentCallBack($paymentKey, $platform) {
        $orderLog = new ChargeOrder();
        $orderLog->log("Anzhi", $_REQUEST);
        //exit();
        ///检测数据;
        $data = $_REQUEST;
        
        if (!isset($data['data'])) {
            $orderLog->log("Anzhi", "Data1 Error");
            return 'fail1';
        }

        ///类对象;
        $des = new DES();
        $code = $des->decrypt($data['data']);

        ///检测数据;
        if (!isset($code) || empty($code)) {
            $orderLog->log("Anzhi", "Data2 Error");
            return 'failed2';
        }

        $response = json_decode($code, true);
        if (!isset($response)) {
            return 'failed';
        }
        $uid = $response["uid"];                        ///安智账号id
        $orderId = $response["orderId"];                ///订单号;
        $orderAmount = $response["orderAmount"];        ///订单金额(单位:分);
        $orderTime = $response["orderTime"];            ///支付时间;
        $orderAccount = $response["orderAccount"];      ///支付账号;
        $code = $response["code"];                      ///订单状态(1:成功);
        $msg = $response["msg"];
        $payAmount = $response["payAmount"];            ///实际支付金额(单位:分)
        $cpInfo = $response["cpInfo"];                  ///回调消息;
        $notifyTime = $response["notifyTime"];          ///通知时间;
        $memo = $response["memo"];                      ///备注;
        $redBagMoney = $response["redBagMoney"];        ///（一） 若用户没有使用不分成礼券支付， 通知没有该字段、或者该字段金额为 0（二）若用户使用包含不分成礼券支付，金额是礼券金额。
        $payAmount = $payAmount / 100;
        $payAmount1 = $orderAmount / 100;
        if ($redBagMoney)
        {
        	$payAmount1 += ($redBagMoney/100);  //加上礼券的金额
        }
		//print_r($response);
        if (!isset($code) || empty($code)) {
            $ret = "failed3";
            $orderLog->log("Anzhi", array("order" => $cpInfo, "money" => $payAmount1, "result" => $code, "serverresult" => "faild"));
        } else {
            ///验证通过;
            if (false == $orderLog->recordOrder(STATUS_SUCCESS, $payAmount1, $cpInfo, "Anzhi", $platform,$orderId)) {
                $ret = "failed4";
                $orderLog->log("Anzhi", array("order" => $cpInfo, "money" => $payAmount1, "result" => $code, "serverresult" => "faild"));
            } else {
                $ret = "success";
                $orderLog->log("Anzhi", array("order" => $cpInfo, "money" => $payAmount1, "result" => $code, "serverresult" => "Success"));
            }
        }
        return $ret;
    }

}

?>
